/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IPaperInfoDAO;
import com.ssyt.examinationsystem.entity.PaperInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class PaperInfoDAOImpl implements IPaperInfoDAO {

    @Override
    public List<Map<String, Object>> getPapers() {
        return JdbcUtils.executeQuery("select * from paperinfo");
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.IPaperInfoDAO#addPaper(com.ssyt.
     * examinationsystem.entity.PaperInfo)
     */
    @Override
    public int addPaper(PaperInfo paper) {
        String sql = "insert into paperInfo values(null,?,?,?,?,?,?,?,?)";
        Object[] parameters = {paper.getPaperName(), paper.getCourse().getCourseId(), paper.getExamTime(),
                paper.getTea().getTeaId(), paper.getPaperDate(), paper.getPassScore(), paper.getRule().getRuleId(),
                paper.getTestId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.IPaperInfoDAO#deletePaper(java.lang.
     * String)
     */
    @Override
    public int deletePaper(String paperId) {
        return JdbcUtils.executeUpdate("delete from paperinfo where paperid=?", paperId);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.IPaperInfoDAO#updatePaper(com.ssyt.
     * examinationsystem.entity.PaperInfo)
     */
    @Override
    public int updatePaper(PaperInfo paper) {
        String sql = "update paperInfo set paperName=?,courseId=?,examTime=?,teaId=?,"
                + "paperDate=?,passScore=?,ruleId=?,testId=? where paperId=?";
        Object[] parameters = {paper.getPaperName(), paper.getCourse().getCourseId(), paper.getExamTime(),
                paper.getTea().getTeaId(), paper.getPaperDate(), paper.getPassScore(), paper.getRule().getRuleId(),
                paper.getTestId(), paper.getPaperId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public Map<String, Object> getPaperInfoById(String paperId) {
        String sql = "select * from paperInfo where paperId = ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, paperId);

        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    /* (non-Javadoc)
     * @see com.ssyt.examinationsystem.dao.IPaperInfoDAO#getPaperName(java.lang.String)
     */
    @Override
    public String getPaperName(String paperId) {
        List<Map<String, Object>> list = JdbcUtils.executeQuery("select papername from paperinfo where paperId=?", paperId);
        return CollectionUtils.isEmptyForList(list) ? String.valueOf(list.get(0).get("paperName")) : "";
    }

}
